package org.code.boot.service.common.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.code.boot.common.utils.DateUtils;
import org.code.boot.common.utils.SecurityUtils;
import org.code.boot.service.system.ISysConfigService;
import org.code.boot.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.code.boot.mapper.common.ComFavoriteMapper;
import org.code.boot.domain.common.ComFavorite;
import org.code.boot.service.common.IComFavoriteService;

/**
 * 通用收藏Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-27
 */
@Service
public class ComFavoriteServiceImpl implements IComFavoriteService
{
    @Autowired
    private ComFavoriteMapper comFavoriteMapper;
    
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询通用收藏
     * 
     * @param favoriteId 通用收藏主键
     * @return 通用收藏
     */
    @Override
    public ComFavorite selectBizFavoriteByFavoriteId(Long favoriteId)
    {
        return comFavoriteMapper.selectBizFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 查询通用收藏列表
     * 
     * @param comFavorite 通用收藏
     * @return 通用收藏
     */
    @Override
    public List<ComFavorite> selectBizFavoriteList(ComFavorite comFavorite)
    {
        return comFavoriteMapper.selectBizFavoriteList(comFavorite);
    }

    /**
     * 新增通用收藏
     * 
     * @param comFavorite 通用收藏
     * @return 结果
     */
    @Override
    public int insertBizFavorite(ComFavorite comFavorite)
    {
        comFavorite.setCreateTime(DateUtils.getNowDate());
        return comFavoriteMapper.insertBizFavorite(comFavorite);
    }

    /**
     * 修改通用收藏
     * 
     * @param comFavorite 通用收藏
     * @return 结果
     */
    @Override
    public int updateBizFavorite(ComFavorite comFavorite)
    {
        comFavorite.setUpdateTime(DateUtils.getNowDate());
        return comFavoriteMapper.updateBizFavorite(comFavorite);
    }

    /**
     * 批量删除通用收藏
     * 
     * @param favoriteIds 需要删除的通用收藏主键
     * @return 结果
     */
    @Override
    public int deleteBizFavoriteByFavoriteIds(Long[] favoriteIds)
    {
        return comFavoriteMapper.deleteBizFavoriteByFavoriteIds(favoriteIds);
    }

    /**
     * 删除通用收藏信息
     * 
     * @param favoriteId 通用收藏主键
     * @return 结果
     */
    @Override
    public int deleteBizFavoriteByFavoriteId(Long favoriteId)
    {
        return comFavoriteMapper.deleteBizFavoriteByFavoriteId(favoriteId);
    }

    /**
     * 收藏/取消收藏
     * 
     * @param businessType 业务类型
     * @param businessId 业务ID
     * @param folderName 收藏夹名称
     * @return 结果
     */
    @Override
    public AjaxResult toggleFavorite(String businessType, Long businessId, String folderName)
    {
        // 检查业务类型是否允许收藏
        String allowTypes = configService.selectConfigByKey("biz.favorite.business.types");
        if (!allowTypes.contains(businessType)) {
            return AjaxResult.error("该业务类型不支持收藏功能");
        }

        // 获取用户信息（收藏功能必须登录）
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        } catch (Exception e) {
            return AjaxResult.error("请先登录");
        }

        // 检查是否已收藏
        ComFavorite existFavorite = comFavoriteMapper.checkFavoriteExists(businessType, businessId, userId);
        
        Map<String, Object> result = new HashMap<>();
        
        if (existFavorite != null) {
            // 取消收藏
            comFavoriteMapper.cancelFavorite(businessType, businessId, userId);
            result.put("favorited", false);
            result.put("message", "取消收藏成功");
        } else {
            // 添加收藏
            ComFavorite comFavorite = new ComFavorite();
            comFavorite.setBusinessType(businessType);
            comFavorite.setBusinessId(businessId);
            comFavorite.setUserId(userId);
            comFavorite.setFolderName(folderName != null ? folderName : "默认收藏夹");
            comFavorite.setCreateBy(SecurityUtils.getUsername());
            comFavorite.setCreateTime(DateUtils.getNowDate());
            
            comFavoriteMapper.insertBizFavorite(comFavorite);
            result.put("favorited", true);
            result.put("message", "收藏成功");
        }
        
        // 获取最新收藏数
        int favoriteCount = comFavoriteMapper.countFavorites(businessType, businessId);
        result.put("favoriteCount", favoriteCount);
        
        return AjaxResult.success(result);
    }

    /**
     * 获取收藏数和收藏状态
     * 
     * @param businessType 业务类型
     * @param businessId 业务ID
     * @return 结果
     */
    @Override
    public AjaxResult getFavoriteInfo(String businessType, Long businessId)
    {
        Map<String, Object> result = new HashMap<>();
        
        // 获取收藏数
        int favoriteCount = comFavoriteMapper.countFavorites(businessType, businessId);
        result.put("favoriteCount", favoriteCount);
        
        // 获取当前用户收藏状态
        boolean favorited = false;
        try {
            Long userId = SecurityUtils.getUserId();
            ComFavorite existFavorite = comFavoriteMapper.checkFavoriteExists(businessType, businessId, userId);
            if (existFavorite != null) {
                favorited = true;
                result.put("folderName", existFavorite.getFolderName());
            }
        } catch (Exception e) {
            // 未登录用户
        }
        
        result.put("favorited", favorited);
        
        return AjaxResult.success(result);
    }
}